﻿@model DLULearningNetwork.ViewModels.PagerViewModel

<script type="text/javascript">
    function OnBegin(loadingID) {        
        var obj = document.getElementById(loadingID);
        $(obj).css("width", $(obj).parent().width());
        $(obj).css("height", $(obj).parent().height());
    }
</script>
           
@{
    //Upto 2 pages
    if (Model.TotalPages > 1)
    {
        // Show First Prev Page
        if (Model.CurrentPage > 1)
        {
            <li>@Ajax.ActionLink(Model.lblFirst, Model.ActionDisplayOnUrl, new { page = 1 },
                    new AjaxOptions
                    {
                        UpdateTargetId = Model.UpdateTargetID,
                        LoadingElementId = Model.LoadingID,
                        Url = Url.Action(Model.Action, new { page = 1 }),
                        OnBegin = "OnBegin('" + Model.LoadingID + "');",
                        OnSuccess = "window.location.hash = '" + 1 + "';"
                    })
            </li>
            <li>@Ajax.ActionLink(Model.lblPrev, Model.ActionDisplayOnUrl, new { page = Model.CurrentPage - 1 },
                    new AjaxOptions
                    {                        
                        UpdateTargetId = Model.UpdateTargetID,
                        LoadingElementId = Model.LoadingID,
                        Url = Url.Action(Model.Action, new { page = Model.CurrentPage - 1 }),
                        OnBegin = "OnBegin('" + Model.LoadingID + "');",
                        OnSuccess = "window.location.hash = '" + (Model.CurrentPage - 1) + "';"
                    })
            </li>   
        }

        /*--Show page numbers--*/

        int start = Model.CurrentPage - (int)(Model.PageSize / 2);
        int end = Model.CurrentPage + (int)((Model.PageSize / 2) - 1);
        if (start < 1)
        {
            start = 1; end = (Model.PageSize > Model.TotalPages) ? Model.TotalPages : Model.PageSize;
        }
        if (end > Model.TotalPages)
        {
            end = Model.TotalPages; start = ((Model.TotalPages - Model.PageSize + 1) > 1) ? (Model.TotalPages - Model.PageSize + 1) : 1;
        }

        for (int i = start; i <= end; i++)
        {
            if (i == Model.CurrentPage)
            {
                <li class="current">@i</li>
            }                
            else
            {
                <li>
                    @Ajax.ActionLink(i.ToString(), Model.ActionDisplayOnUrl, new { page = i },
                        new AjaxOptions
                        {
                            UpdateTargetId = Model.UpdateTargetID,
                            LoadingElementId = Model.LoadingID,
                            Url = Url.Action(Model.Action, new { page = i }),
                            OnBegin = "OnBegin('" + Model.LoadingID + "');",
                            OnSuccess = "window.location.hash = '" + i + "';"
                        })
                </li>               
            }
        }

        /*---Show Next Last Page--*/
        if (Model.CurrentPage < Model.TotalPages)
        {
                <li>
                    @Ajax.ActionLink(Model.lblNext, Model.ActionDisplayOnUrl, new { page = Model.CurrentPage + 1 },
                        new AjaxOptions
                        {
                            UpdateTargetId = Model.UpdateTargetID,
                            LoadingElementId = Model.LoadingID,
                            Url = Url.Action(Model.Action, new { page = Model.CurrentPage + 1 }),
                            OnBegin = "OnBegin('" + Model.LoadingID + "');",
                            OnSuccess = "window.location.hash = '" + (Model.CurrentPage + 1) + "';"
                        })
                </li>  
                <li>
                    @Ajax.ActionLink(Model.lblLast, Model.ActionDisplayOnUrl, new { page = Model.TotalPages },
                        new AjaxOptions
                        {
                            UpdateTargetId = Model.UpdateTargetID,
                            LoadingElementId = Model.LoadingID,
                            Url = Url.Action(Model.Action, new { page = Model.TotalPages }),
                            OnBegin = "OnBegin('" + Model.LoadingID + "');",
                            OnSuccess = "window.location.hash = '" + Model.TotalPages + "';"
                        })
                </li>              
        }
    }
}